//
//  MyXiaZaiViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/3/2.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "MyXiaZaiViewController.h"
#import "GameViewController.h"
@interface MyXiaZaiViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) BaseTableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@end

@implementation MyXiaZaiViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
        
    }
    return self;
}

- (void)dealloc{
    [_tableView release];
    [_arr release];
    [_model release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我的下载";
    self.view.backgroundColor = [UIColor whiteColor];
    //获取document路径
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    //拼接下载路径
    NSString *xiaZaiWenJian = [document stringByAppendingPathComponent:@"xiaZaiWenJianJia.plist"];
    //写入本地
    
    self.arr = [NSMutableArray array];
    
    
  NSArray *arr  = [NSMutableArray arrayWithContentsOfFile:xiaZaiWenJian];
    
    
    if (arr.count == 0) {
        UIImageView *image = [[UIImageView alloc] init];
        image.frame = CGRectMake(0, 0, 100, 100);
        image.center = self.view.center;
        image.image = [UIImage imageNamed:@"iconfont-yewuyeicon05.png"];
        
        
        [self.view addSubview:image];
        [image release];

    }else{
        self.tableView = [[BaseTableView alloc] initWithFrame:self.view.bounds];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [self.view addSubview:_tableView];
        
        [_tableView release];
        
        [_tableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    }
    
    
    //穿化为集合去掉重复的
    NSSet *set = [NSSet setWithArray:arr];
    
    for (NSMutableDictionary *dic in set) {
        [_arr addObject:dic];
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    
    NSDictionary *dic = _arr[indexPath.row];
    NSString *image = dic[@"image"];
    
    cell.imageView.frame = CGRectMake(10, 5, 100, 50);
    cell.imageView.layer.cornerRadius = 5;
    cell.imageView.layer.masksToBounds = YES;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:PLACE_HLODER]];
    
    cell.textLabel.text = dic[@"name"];
    
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dic = _arr[indexPath.row];
    
    GameViewController *game = [[GameViewController alloc] init];
    game.tripId = dic[@"urlId"];
    [self.navigationController pushViewController:game animated:YES];
    
    [game release];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
